package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.BjclasstypeTwo;
import com.zmht.beike.po.StudentTwo;
import com.zmht.beike.po.TCognition;
import com.zmht.beike.po.THabit;
import com.zmht.beike.po.THealthmovement;
import com.zmht.beike.po.TLanguage;
import com.zmht.beike.po.TSociality;
import com.zmht.specedu.vo.Teacher;

public class StudentAction extends BaseAction {

	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward getAllStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Integer index = 1;
			Integer size = 10;
			if(null!=request.getParameter("currPage")){
				index = new Integer(request.getParameter("currPage"));
			}
			if(null!=request.getParameter("pageSize")){
				size = new Integer(request.getParameter("pageSize"));
			}
			String stuName = request.getParameter("stuName");
			String className = request.getParameter("className");
			//List list = this.getStudentBiz().getStudentDAO().findByPage("from StudentTwo", index-1, size);//findByHQLObj("from StudentTwo");
			
			StudentTwo stu = new StudentTwo();
			stu.setSname(stuName);
			
			
			if(null!=className&&!"".equals(className)){
			BjclasstypeTwo type =	new BjclasstypeTwo();
			type.setTypeName(className);
			stu.setBjclasstype(type);
			}
			List list = this.getStudentBiz().getStudentDAO().findByCriteria(stu, index-1, size);
			Integer total =this.getStudentBiz().getStudentDAO().findByCriteriaTotal(stu, index, size);
		
			
			request.setAttribute("currPage", index); //��ǰҳ��
			request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(size)));//��ҳ��
			request.setAttribute("pageSize", size);// ÿҳ��С
			
			request.setAttribute("students", list);
			request.setAttribute("stuName", stuName);
			request.setAttribute("className", className);
			return mapping.findForward("student_list");
	}
	/**
	 * * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			String id = request.getParameter("studentId");
			StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(id));
			request.setAttribute("stu", student);
		return mapping.findForward("view_student");
	}
	/**
	 * 获得评测结果中学生列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getPinceStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			
			Integer index = 1;
			Integer size = 10;
			if(null!=request.getParameter("currPage")){
				index = new Integer(request.getParameter("currPage"));
			}
			if(null!=request.getParameter("pageSize")){
				size = new Integer(request.getParameter("pageSize"));
			}
			
			StudentTwo stu = new StudentTwo();
			stu.setSname(request.getParameter("stuName"));
			
			
			List list = this.getStudentBiz().getStudentDAO().findByCriteria(stu, index-1, size);
			Integer total =this.getStudentBiz().getStudentDAO().findByCriteriaTotal(stu, index, size);
			
			
			request.setAttribute("currPage", index);
			request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(size)));
			request.setAttribute("pageSize", size);
			request.setAttribute("students", list);
			request.setAttribute("stuName", request.getParameter("stuName"));
		return mapping.findForward("pingcejieguo");
	}
	/**
	 * 获得评测结果用于编辑
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getPingCeStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String action = request.getParameter("action");
		
		String id = request.getParameter("studentId");
		StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(id));
		request.setAttribute("stu", student);
		
		request.setAttribute("tname", teacher.getTname());
		//编辑 或者添加 edit 或 add
		request.getSession().setAttribute("action", action);
		return mapping.findForward("gerenfazhan_edit");
	}
	/**
	 * 显示评测结果
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showCeStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			String id = request.getParameter("studentId");
			StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(id));
			request.setAttribute("stu", student);
			
			TLanguage lg = this.getLanguageBiz().findByStudentId(id);
			TCognition tc = this.getCognitionBiz().findByStudentId(id);
			THabit th = this.getHabitBiz().findByStudentId(id);
			THealthmovement thm = this.getHealthMovementBiz().findByStudentId(id);
			TSociality ts = this.getSocialityBiz().findByStudentId(id);
			
			request.setAttribute("language",lg);
			request.setAttribute("cognition", tc);
			request.setAttribute("habit", th);
			request.setAttribute("healthmovement", thm);
			request.setAttribute("sociality", ts);
			
			
		return mapping.findForward("gerenfazhan_view");
	}
}
